package net.bwie.realtime.vehicle.job;

import ch.hsr.geohash.GeoHash;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.bwie.realtime.vehicle.utils.KafkaUtil;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.table.functions.ScalarFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

/**
 * @Author: FuHe
 * @Date: 2025/5/27
 */
public class VehicleLogEtlJob {
    public static void main(String[] args) throws Exception {
        // 执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        // 读取kafka数据
        DataStream<String> kafkaStream = KafkaUtil.consumerKafka(env, "vehicle-data");
//        kafkaStream.print();

        DataStream<String> etlStream = ETLStream(kafkaStream);
//        etlStream.print();
        DataStream<String> geohashLog = geohashStream(etlStream);

//        VehicleLogClickHouse.sinkToClickhouse(geohashLog);

        KafkaUtil.producerKafka(geohashLog, "vehicle-date-geohash");

        env.execute("VehicleLogEtlJob");
    }



    private static DataStream<String> geohashStream(DataStream<String> etlStream) {
        SingleOutputStreamOperator<String> geohashProcess = etlStream.process(
                new ProcessFunction<String, String>() {
                    @Override
                    public void processElement(String value, Context ctx, Collector<String> out) throws Exception {
                        JSONObject jsonObject = JSON.parseObject(value);
                        Double longitude = jsonObject.getDouble("longitude");
                        Double latitude = jsonObject.getDouble("latitude");
                        SubstringFunction substringFunction = new SubstringFunction();
                        String geohash = substringFunction.evaluate(longitude, latitude, 6);
                        jsonObject.put("geohash", geohash);
                        out.collect(jsonObject.toJSONString());
                    }
                }
        );
        return geohashProcess;
    }
    private static DataStream<String> ETLStream(DataStream<String> kafkaStream) {
        OutputTag<String> dirtyTag = new OutputTag<String>("dirty-log") {
        };
        SingleOutputStreamOperator<String> process = kafkaStream.process(
                new ProcessFunction<String, String>() {
                    @Override
                    public void processElement(String value, ProcessFunction<String, String>.Context ctx, Collector<String> out) {
                        try {
                            JSON.parseObject(value);
                            out.collect(value);
                        } catch (Exception e) {
                            ctx.output(dirtyTag, value);
                        }
                    }
                });
        DataStream<String> dirty = process.getSideOutput(dirtyTag);
        KafkaUtil.producerKafka(dirty, "vehicle-log-dirty");
        return process;
    }

    // 定义geohash函数
    public static class SubstringFunction extends ScalarFunction {
        public String evaluate(double jd, double wd, int cd) {
            GeoHash geoHash = GeoHash.withCharacterPrecision(wd, jd, cd);
            return geoHash.toBase32();
        }
    }

}
